<!--index.html-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
  <meta http-equiv="Content-Security-Policy" content="default-src * self blob: data: gap:; style-src * self 'unsafe-inline' blob: data: gap:; script-src * 'self' 'unsafe-eval' 'unsafe-inline' blob: data: gap:; object-src * 'self' blob: data: gap:; img-src * self 'unsafe-inline' blob: data: gap:; connect-src self * 'unsafe-inline' blob: data: gap:; frame-src * self blob: data: gap:;">
  <title>API Testing</title>
</head>
<body>

<div style="margin: 5px">
  <div>
    <div>Server Status</div>
    <button type="button" id="action">Start</button>
    <button type="button" id="open-server-page">Open Server Page</button>
    <div>
      <span>Port:</span><input name="port" id="port" type="text"/>
    </div>
  </div>

  <div>
    <div>Log</div>
    <button type="button" id="open-log-file">Open Log File</button>
  </div>
</div>

<script>
const actionBut = document.getElementById('action');
actionBut.addEventListener('click', (e) => {
    const action = actionBut.innerHTML;
    switch (action) {
        case 'Stop':
            window.electronAPI.stopServer()
            break;
        case 'Start':
            window.electronAPI.startServer()
            break;
    }
})

const openServerBut = document.getElementById('open-server-page');
openServerBut.addEventListener('click', async (e) => {
  window.location = await window.electronAPI.getHomePage()
})

const openLogfileBut = document.getElementById('open-log-file')
openLogfileBut.addEventListener('click', () => {
  window.electronAPI.openLogDir()
})

const loadServerStatus = async () => {
  const healthzUrl = await window.electronAPI.getHealthzUrl()

  fetch(healthzUrl).then(res => {
    actionBut.innerHTML = 'Stop';
  }).catch(err => {
    actionBut.innerHTML = 'Start';
  })
}

loadServerStatus()
window.setInterval(loadServerStatus, 2000)

const portInput = document.getElementById('port');
(async function() {
  portInput.value = await window.electronAPI.getPort()
})();
</script>
</body>
</html>